<?php

namespace Admin\Controller;

class CityController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("City");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    /**
     * 添加数据
     */
    public function addPost() {

        $cityName = $_REQUEST["cityName"];

        $cityMap["city_name"] = $cityName;

        $city = $this->currentModel->queryByMap($cityMap);

        if(!isNullOrEmpty($city)) {
            $this->error("对不起,名称重复,请重新填写");
        }

        $cityData["city_name"] = $cityName;
        $cityData["create_time"] = getCurrentDate();

        $this->currentModel->add($cityData);

        $this->success("添加成功", U("city/index"));

    }

    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];

        $city = $this->currentModel->queryById($id);

        $cityName = $_REQUEST["cityName"];

        $cityMap["city_name"] = $cityName;
        $cityMap["city_id"] = array("neq", $city["city_id"]);

        $repeatCity = $this->currentModel->queryByMap($cityMap);

        if(!isNullOrEmpty($repeatCity)) {
            $this->error("对不起,名称已经存在,请重新填写");
        }

        $city["city_name"] = $cityName;

        $this->currentModel->updateInfo($city);

        $this->success("更新成功", U("city/index"));

    }


}